package com.sae.vote.controller;

import java.sql.SQLException;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sae.vote.entity.Items;
import com.sae.vote.entity.Subject;
import com.sae.vote.entity.Users;
import com.sae.vote.service.SubjectService;

/**
 * 投票相关操作
 * 
 */
@Controller
@RequestMapping
public class VoteController {

	@Autowired
	private SubjectService subjectService;

	@RequestMapping(value = "/vote", method = RequestMethod.GET)
	public String vote(HttpServletRequest request) throws SQLException {
		return "vote/vote";
	}

	/**
	 * 新建投票
	 * 
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/vote", method = RequestMethod.POST)
	public String save(HttpServletRequest request) throws SQLException {
		String title = request.getParameter("subjectTitle");
		String desc = request.getParameter("descript");
		String img = getImage(desc);
		Subject subject = new Subject();
		subject.setBeginTime(new Date());
		subject.setCreator(((Users) request.getSession().getAttribute("user"))
				.getUserID());
		subject.setIsAnonym((byte) 1);
		subject.setState((byte) 1);
		subject.setSubjectTitle(title);
		subject.setCover(img);
		subject.setDescript(desc);
		String subjectID = subjectService.save(subject);
		// 保存选项
		for (int i = 1; i < 21; i++) {
			String item = request.getParameter("item" + i);
			if (!StringUtils.isEmpty(item)) {
				Items items = new Items();
				items.setItem(item);
				items.setNo(i);
				items.setSubjectID(subjectID);
				subjectService.saveItem(items);
			}
		}
		return "vote/vote";
	}

	/**
	 * 获得投票
	 * 
	 * @param id
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/vote/{id}/detail", method = RequestMethod.GET)
	public String getVote(@PathVariable("id") String id,
			HttpServletRequest request) throws SQLException {
		Subject subject = subjectService.findById(id);
		request.setAttribute("subject", subject);
		return "vote/detail";
	}

	/**
	 * AJAX方式进行投票
	 * 
	 * @param id
	 *            选项
	 * @param request
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/item/{id}", method = RequestMethod.POST)
	public @ResponseBody
	boolean vote(@PathVariable("id") String id, HttpServletRequest request)
			throws SQLException {
		Users user = (Users) request.getSession().getAttribute("user");
		subjectService.vote(id, user.getUserID());
		return true;
	}

	/**
	 * 获得正文中的图片地址
	 * 
	 * @param content
	 * @return
	 */
	public String getImage(String content) {
		Matcher m = Pattern.compile("src=\"?(.*?)(\"|>|\\s+)").matcher(content);
		while (m.find()) {
			return m.group(1);
		}
		return null;
	}
}
